#include "fun.h"
#include "../sup/sup.h"
#include<stdio.h>
#include<stdlib.h>
void init(int course_count, int pupil_count) {
    int i,j;
    _courses_arr[MAX_course] = (struct course *) malloc(course_count*sizeof(struct course));
    _pupils_arr[MAX_pupil] = (struct pupil *) malloc(pupil_count*sizeof(struct pupil));
    _courses_arr[MAX_course]->count=0;//将所有结构体初始化为0
    _pupils_arr[MAX_pupil]->count=0;
    for(i=0;i<COURSE_COUNT;i++){//方便以后判断,为NULL就是没有课
        _courses_arr[MAX_course]->pupils[i]=NULL;
    }
    for(j=0;j<PUPIL_COUNT;j++){
        _pupils_arr[MAX_pupil]->courses[i]=NULL;
    }
}
int xuanke(char *course_name,char *pupil_name){
    int i=0,j=0,k=0;
    for(i=0;i<MAX_pupil;i++){
        if(str_cmp(_pupils_arr[i]->name,pupil_name)==0){//先匹配人名
            for(j=0;j<MAX_course;j++){
                if(str_cmp(_courses_arr[j]->name,course_name)==0){//在匹配课程名
                    for ( k = 0; k <_pupils_arr[i]->count ; k++){//判断是否重复
                        if(str_cmp((_pupils_arr[i]->courses[k]->name),(course_name))==0){
                            printf("%s\tthis pupils has this courses %s\n",pupil_name,course_name);
                            return 1;
                        }
                    }
                    if(_pupils_arr[i]->count<PUPIL_COUNT && _courses_arr[j]->count<COURSE_COUNT ){//判断课程或者人数是否已经最大了
                        _courses_arr[j]->pupils[_courses_arr[j]->count]=_pupils_arr[i];//直接以count作为结构体指针的下标
                        _pupils_arr[i]->courses[_pupils_arr[i]->count]=_courses_arr[j];//直接以count作为结构体指针的下标
                        //printf("%s%s",_pupils_arr[i]->name,_courses_arr[j]->name);
                        _pupils_arr[i]->count++;
                        _courses_arr[j]->count++;
                    }
                    else{
                        if(_pupils_arr[i]->count<PUPIL_COUNT){
                            printf("%s de courses is full!!\n",_pupils_arr[i]->name);
                            return 1;
                        }
                        else{
                            printf("%s de pupils is full!!\n",_courses_arr[j]->name);
                        }
                    }
                }
            }
            if(j>MAX_course){
                printf("%s this name is not found!\n",pupil_name);
            }
        }

    }
    if(i>MAX_pupil){
        printf("%s this name is not found!\n",course_name);
    }
}
int xiugai(char *course_name,char *new_course_name,char *pupil_name){
    tuike(course_name,pupil_name);
    xuanke(new_course_name,pupil_name);
}
int tuike(char *course_name,char *pupil_name){
    int i=0,j=0,k=0;
    int biaoji1,biaoji2;
    for(i=0;i<MAX_course;i++){
        if(str_cmp(_courses_arr[i]->name,course_name)==0){//匹配课程名
            for(j=0;j<_courses_arr[i]->count;j++){
                if(str_cmp((_courses_arr[i]->pupils[j]->name),pupil_name)==0){//在该课程下匹配学生姓名
                    _courses_arr[i]->pupils[j]=NULL;
                        for (k = j+1; k< PUPIL_COUNT; k++){
                            if(_courses_arr[i]->pupils[k]!=NULL){//如后后面有学生，把后面的往前面移动，确保count为下标
                                _courses_arr[i]->pupils[k-1]=_courses_arr[i]->pupils[k];
                                _courses_arr[i]->pupils[k]=NULL;
                            }
                        }
                    _courses_arr[i]->count--;
                }
            }
            if(j==0){
                printf("sorry %s don't have %s\n",pupil_name,course_name);
                return 1;
            }
        }
    }
    for (i=0;i<MAX_pupil;i++) {
        if(str_cmp(_pupils_arr[i]->name,pupil_name)==0)
            for(j=0;j<_pupils_arr[i]->count;j++){
                    if(str_cmp((_pupils_arr[i]->courses[j]->name),course_name)==0){
                        _pupils_arr[i]->courses[j]=NULL;
                        for (k = j+1; k< COURSE_COUNT; k++){
                            if(_pupils_arr[i]->courses[k]!=NULL){
                                _pupils_arr[i]->courses[k-1]=_pupils_arr[i]->courses[k];
                                _pupils_arr[i]->courses[k]=NULL;
                            }
                        }
                        _pupils_arr[i]->count--;
                        }
            }
             if(j==0){
                printf("sorry %s don't have %s\n",pupil_name,course_name);
                return 1;
            }
    }
    //zhengli(_courses_arr[biaoji1],_pupils_arr[biaoji2]);
}
int lookall(){
    int i,j;
    int _courses_arr_cont;
    for(i=0;i<MAX_course;i++){
        if(_courses_arr[i]->count>0){
            printf("%s课程的学生有：",_courses_arr[i]->name);
            _courses_arr_cont=_courses_arr[i]->count;
            printf("%d\n",_courses_arr_cont);
            for(j=0;j<PUPIL_COUNT;j++){
                if(_courses_arr[i]->pupils[j]!=NULL){
                    printf("%s",_courses_arr[i]->pupils[j]->name);
                }
            }
            printf("\n");
        }
    }
}
void lookpupil(char *pupil_name){
    int i,j;
    for(i=0;i<MAX_pupil;i++){
        if(str_cmp(_pupils_arr[i]->name,pupil_name)==0){
            printf("%s have this courses : %d\n",pupil_name,_pupils_arr[i]->count);
            for(j=0;j<_pupils_arr[i]->count;j++){
                    printf("%s",_pupils_arr[i]->courses[j]->name);
            }
        }
    }
}
void lookcouse(char *course_name){
    int i,j;
    for(i=0;i<MAX_course;i++){
        if(str_cmp(_courses_arr[i]->name,course_name)==0){
            printf("%s have this pupils : %d\n",course_name,_courses_arr[i]->count);
            for(j=0;j<_courses_arr[i]->count;j++){
                printf("%s",_courses_arr[i]->pupils[j]->name);
            }
        }
    }
}
